import com.adobe.serialization.json.JSON;
import com.flex.core.constants.GameConstants;
import com.flex.core.util.img.ImgUtils;

import flash.events.Event;
import flash.net.URLRequest;
import flash.net.URLStream;

import mx.containers.Canvas;
import mx.controls.Alert;

/**
 * @author sam
 * this is a function to do something base
 */


/**
 * read property ,init base imgList
 */
public function readProperty(url:String):void
{
	try
	{
		var loader:URLStream=new URLStream();
		loader.addEventListener(Event.COMPLETE, getHttpReq);
		var request:URLRequest=new URLRequest(url);
		loader.load(request);
	}
	catch (err:Error)
	{
		Alert.show("property connection error:" + err.message);

	}
}

/**
 * read mapInfo from txt
 */
public function getHttpReq(e:Event):void
{
	try
	{
		var us:URLStream=e.target as URLStream;
		var str:String=us.readMultiByte(us.bytesAvailable, "UTF-8");
		var mapArray:Array=new Array();
		var map:Object=null;
		var url:String="";
		if (str != "" && str.replace(" ", "").length > 0)
		{
			mapArray=JSON.decode(str) as Array;
			if (mapArray != null && mapArray.length > 0)
			{
				for (var i:Number=0; i < mapArray.length; i++)
				{
					var imgPathTemp:String=mapArray[i]["mapPath"];
					var pathArray:Array=imgPathTemp.split("/");
					if (pathArray[pathArray.length - 1] == GameConstants.CUR_MAP_NAME)
					{
						url=imgPathTemp;
						map=mapArray[i];
						GameConstants.CUR_MAP_OBJECT=map;
						GameConstants.USER_CUR_MAP_WIDTH=Number(map["mapWidth"]);
						GameConstants.USER_CUR_MAP_HEIGHT=Number(map["mapHeight"]);
						break;
					}
				}
			}
		}
		GameConstants.BASE_MAP_CANVAS=(menu_info.getChildAt(0) as Canvas);
		GameConstants.BASE_MAP_CANVAS.addChild(ImgUtils.getImage(url, "resizeChangedMap"));
//		GridUtil.createChangedMapGrid(url, menu_info.getChildAt(1) as Canvas, 1, map);
	}
	catch (err:Error)
	{
		Alert.show("request error:" + err.message);
	}
}

/**
 * create base menu for game editer
 */
public function createBaseMenu():void
{
//	GameConstants.BASE_ELE_MAP.puts(menu_main.id, menu_main);
//	GameConstants.BASE_ELE_MAP.puts(menu_map_list.id, menu_map_list);
	GameConstants.BASE_ELE_MAP.puts(menu_info.id, menu_info);
//	GameConstants.BASE_ELE_MAP.puts(menu_map_create.id, menu_map_create);
}